package com.higgins.eventtracking.client.health.record;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.FinanceTrackingService;
import com.higgins.eventtracking.client.FinanceTrackingServiceAsync;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.DynamicTypeDTO;
import com.higgins.eventtracking.client.health.action.ActionTab;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;

public class RulesTab extends VerticalPanel 		{
	private final FinanceTrackingServiceAsync financeTrackingService = GWT
														.create(FinanceTrackingService.class);
	private boolean firstRun = true;
	private TabPanel tabPanel = new TabPanel();
	private GenericTabInterface parentTab = null;
	private List<RulesSubTab> subTabs = new ArrayList<RulesSubTab>();
	private String dynamicType = null;
	
	public RulesTab(GenericTabInterface parentTab, String dynamicType)	{
		this.parentTab = parentTab;
		this.dynamicType = dynamicType;
		setHeight("35em");
	}
	public String getDynamicType() {
	    return dynamicType;
	}
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		populateTabPanel();
		firstRun = false;
	}
	public void populateTabPanel()	{
		retrieveFramesOfMind();
	}
	public SelectionHandler<Integer> createSelectionHandler()	{
		SelectionHandler<Integer> selectionHandler = new SelectionHandler<Integer>() {
	        @Override
	        public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
	        	RulesSubTab selectedSubTab = subTabs.get(integerSelectionEvent.getSelectedItem());
	        	selectedSubTab.update();
	        }
		};
		return selectionHandler;
	}
	
	private void retrieveFramesOfMind()	{
		final List<String> list = new ArrayList<String>();
		try {
			financeTrackingService.retrieveDynamicTypes(dynamicType, new AsyncCallback<List<DynamicTypeDTO>>()  {

				@Override
				public void onFailure(Throwable caught) {

				}

				@Override
				public void onSuccess(List<DynamicTypeDTO> result) {
					tabPanel.clear();
					for (DynamicTypeDTO dto:result)	{
						if (dto.getSubType()==null || dto.getSubType().equals(false))	{
							list.add(dto.getDynamicType());
						}
					}
					addTabs(list);
					add(tabPanel);
					if (subTabs.size()>0)	{
						subTabs.get(0).update();
					}
					tabPanel.addSelectionHandler(createSelectionHandler());
				} });
		} catch (IllegalArgumentException e) {

		} catch (SecurityAccessDeniedException e) {

		}	
	}
	
	private void addTabs(List<String> frameOfMinds)	{
		for (String frameOfMind:frameOfMinds)	{
			RulesSubTab subTab = new RulesSubTab(this, frameOfMind);
			subTabs.add(subTab);
			tabPanel.add(subTab, frameOfMind);
		}
		tabPanel.selectTab(0);
	}
}
